package com.comtrade.activities;

import java.util.concurrent.ExecutionException;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.comtrade.ilserver.tasks.UserRegistrationTask;
import com.example.beaconapk.R;

public class RegisterActivity extends Activity {

	private Button btn;
	private EditText et_userName;
	private EditText et_password;
	private EditText et_firstName;
	private EditText et_lastName;
	public static final String MyPREFERENCES = "MyPrefs";
	public static final String URL = "URL";
	public static final String Name = "Username";
	public static final String Password = "Passwd";
	private static String url = "";
	SharedPreferences sharedepreference;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		getActionBar().setHomeButtonEnabled(true);

		btn = (Button) findViewById(R.id.btn_register);
		et_userName = (EditText) findViewById(R.id.et_userName);
		et_password = (EditText) findViewById(R.id.et_password);
		et_firstName = (EditText) findViewById(R.id.et_firstName);
		et_lastName = (EditText) findViewById(R.id.et_lastName);

		sharedepreference = getSharedPreferences(MyPREFERENCES,
				Context.MODE_PRIVATE);
		if (sharedepreference.contains(URL)) {
			url = "http://"+sharedepreference.getString(URL, "")+":8080/ils/rest/register";
		}
		Toast.makeText(
				RegisterActivity.this,
				sharedepreference.getString(URL, "") + " "
						+ sharedepreference.getString(Name, "")
						+ sharedepreference.getString(Password, ""),
				Toast.LENGTH_SHORT).show();
		btn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if ((et_userName.getText().toString().isEmpty())
						|| (et_password.getText().toString().isEmpty())
						|| (et_firstName.getText().toString().isEmpty())
						|| (et_lastName.getText().toString().isEmpty())) {
					Toast.makeText(RegisterActivity.this,
							"Niste uneli trazene parametre!",
							Toast.LENGTH_SHORT).show();
				} else {
					UserRegistrationTask urt = new UserRegistrationTask();
					urt.setFirstName(et_firstName.getText().toString());
					urt.setPassword(et_password.getText().toString());
					urt.setSurname(et_lastName.getText().toString());
					urt.setUsername(et_userName.getText().toString());
					urt.execute(url, urt.getUsername(), urt.getPassword(),
							urt.getFirstName(), urt.getSurname());
					try {
						Log.d("URL",url);
						Log.d("URT",urt.getFirstName());
						if (urt.get() != null) {
							Toast.makeText(RegisterActivity.this,
									"Uspesna registracija!", Toast.LENGTH_SHORT)
									.show();
							finish();
						} else {
							Toast.makeText(RegisterActivity.this,
									"Registracija nije uspela!",
									Toast.LENGTH_SHORT).show();
						}
					} catch (InterruptedException e) {
						e.printStackTrace();
					} catch (ExecutionException e) {
						e.printStackTrace();
					}

					String n = et_userName.getText().toString();
					String ph = et_password.getText().toString();
					Editor editor = sharedepreference.edit();
					editor.putString(Name, n);
					editor.putString(Password, ph);
					editor.commit();
				}

			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}